﻿/*  
 * Helix Engine
 * http://helixengine.codeplex.com/
 * 
 * Copyright (c) 2010 - 2011 Joe Hegarty
 *  
 * This source code is subject to the terms of the Microsoft Public License (Ms-PL). 
 *  
 * Redistribution and use in source and binary forms, with or without modification, 
 * is permitted provided that redistributions of the source code retain the above 
 * copyright notices and this file header. 
 * 
 * For details please see http://www.opensource.org/licenses/ms-pl.html 
 *  
 * All other rights reserved. 
 *
 */  


using System;

using HelixEngine;
using HelixEngine.Maths;

namespace HelixGame.World.Entities.Components
{
    /// <summary>
    /// 
    /// </summary>
    public class ObjectPlacerComponent : EntityComponent
    {
        Entity _floorEntity;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="floorEntity"></param>
        public ObjectPlacerComponent(Entity floorEntity)
        {
            _floorEntity = floorEntity;
        }

        /// <summary>
        /// 
        /// </summary>
        public override void OnInitialised()
        {
            Parent.AddComponentNeedingUpdate(this);
        }

        /// <summary>
        /// 
        /// </summary>
        public override void OnCleanup()
        {
            Parent.RemoveComponentNeedingUpdate(this);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="time"></param>
        public override void Update(EngineTime time)
        {
            if (Engine.Get().InputManager.IsMouseClick())
            {
                FloorComponent floorComponent = null;
                if (_floorEntity.GetEC(ref floorComponent))
                {
                    Vector2 mousePoint = Engine.Get().InputManager.GetMousePoint();
                    IsometricPoint placeTile = IsometricHelper.ScreenSpaceToWorldSpace(mousePoint);

                    if (floorComponent.IsTileValid(placeTile))
                    {
                        StaticObjectComponent staticObject = new StaticObjectComponent("block", placeTile, 0);
                        Game.Get().WorldManager.AddEntity("Block").AddEC(staticObject);
                    }
                }
            }
        }
    }
}
